package com.edu.robot.modules.robot.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.robot.model.R;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import com.edu.robot.modules.robot.controller.vo.ModelConfigVO;
import com.edu.robot.modules.robot.controller.vo.PageQuery;
import com.edu.robot.modules.robot.controller.vo.QueryModelVO;
import com.edu.robot.modules.robot.corvert.ModelConfigCovert;
import com.edu.robot.modules.robot.dto.ModelConfigDTO;
import com.edu.robot.modules.robot.dto.PageResult;
import com.edu.robot.modules.robot.mapper.CrModelConfigMapper;
import com.edu.robot.modules.robot.model.CrModelConfig;
import com.edu.robot.modules.robot.service.CrModelConfigService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @author 86177
* @description 针对表【cr_model_config】的数据库操作Service实现
* @createDate 2025-02-22 14:34:38
*/
@Service
public class CrModelConfigServiceImpl extends ServiceImpl<CrModelConfigMapper, CrModelConfig>
    implements CrModelConfigService {

	@Override
	public R<Boolean> createModelConfig(ModelConfigDTO modelConfigDTO) {

		if(Objects.isNull(modelConfigDTO)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"模型配置信息不能为空！");
		}

		CrModelConfig crModelConfig = ModelConfigCovert.covert(modelConfigDTO);

		boolean save = save(crModelConfig);

		return R.success(save);
	}

	@Override
	public ModelConfigDTO queryConfigById(Long id) {

		if(null == id){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"模型配置id不能为空！");
		}

		CrModelConfig byId = getById(id);

		if(Objects.isNull(byId)){
			throw new BizException(ResultCodeEnum.DATA_NOT_FOUND,"模型配置不存在！");
		}

		return ModelConfigCovert.covert(byId);

	}

	@Override
	public PageResult<ModelConfigDTO> queryModelList(PageQuery<QueryModelVO> queryModel) {

		if(Objects.isNull(queryModel)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"查询条件不能为空！");
		}

		LambdaQueryWrapper<CrModelConfig> queryWrapper = new LambdaQueryWrapper<>();

		//分页参数没有设置默认值
		Long currentPage = Optional.ofNullable(queryModel.getPageNum()).orElse(1L);
		Long pageSize = Optional.ofNullable(queryModel.getPageSize()).orElse(10L);

		QueryModelVO query = queryModel.getQuery();

		if(Objects.nonNull(query)){
			if(StrUtil.isNotBlank(query.getModelName())){
				queryWrapper.like(CrModelConfig::getModelName, query.getModelName());
			}

			if (StrUtil.isNotBlank(query.getModelId())){
				queryWrapper.eq(CrModelConfig::getId, query.getModelId());
			}

			if (StrUtil.isNotBlank(query.getTopP())){
				queryWrapper.eq(CrModelConfig::getTopP, query.getTopP());
			}

			if (StrUtil.isNotBlank(query.getTopK())){
				queryWrapper.eq(CrModelConfig::getTopK, query.getTopK());
			}

			if (StrUtil.isNotBlank(query.getTemperature())){
				queryWrapper.eq(CrModelConfig::getTemperature, query.getTemperature());
			}

		}

		Page<CrModelConfig> page = page(new Page<>(currentPage, pageSize), queryWrapper);

		if(page == null){
			throw new BizException(ResultCodeEnum.DATA_NOT_FOUND,"模型配置不存在！");
		}

		PageResult<ModelConfigDTO> pageResult = new PageResult<>();
		pageResult.setTotal(page.getTotal());
		pageResult.setCurrentPage(page.getCurrent());
		pageResult.setTotalPages(page.getPages());

		if(CollectionUtil.isNotEmpty(page.getRecords())){
			List<ModelConfigDTO> collect = page.getRecords().stream().map(ModelConfigCovert::covert).toList();
			pageResult.setRecords(collect);

			return pageResult;
		}

		return pageResult;
	}

	@Override
	public R<Boolean> updateModelConfig(ModelConfigVO modelConfigVO) {
		if(Objects.isNull(modelConfigVO)){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"模型配置信息不能为空！");
		}

		//模型Id不能为null
		if(StrUtil.isBlank(modelConfigVO.getId())){
			throw new BizException(ResultCodeEnum.PARAM_INVALID,"模型Id不能为空！");
		}

		CrModelConfig crModelConfig = ModelConfigCovert.covertToDO(modelConfigVO);

		if(updateById(crModelConfig)){
			return R.success(true);
		}

		return R.fail(ResultCodeEnum.FAIL);
	}
}




